﻿using System;
using System.Linq;
using DependencyAnalysis.DataModel;
using theeg.MatrixWpf.Models;

namespace theeg.DependencyAnalysis.CommonAddin.Models
{
    public class AdjacencyMatrixModel : ViewModelBase
    {
        private Element[] _matrixElements;

        public Element[] MatrixElements
        {
            get { return _matrixElements; }
            set
            {
                SetNotifyingField(ref _matrixElements, value, "MatrixElements");
            }
        }

        private IDependencyModel _model;

        public void CalculateFromModel(IDependencyModel model)
        {
            _model = model;
            Func<TypeDescriptor, Element[]> convertMembers = t => (from x in t.Members select new Element { Text = x.Name, Tag = x }).ToArray();
            Func<SubmoduleDescriptor, Element[]> convertTypes = s => (from x in s.Types select new Element { Text = x.Name, Tag = x, Children = convertMembers(x) }).ToArray();
            Func<ModuleDescriptor, Element[]> convertSub = m => (from x in m.Submodules select new Element { Text = x.Name, Tag = x, Children = convertTypes(x) }).ToArray();

            MatrixElements = (from x in model.DependenciesPerModuleOut.Keys
                              select new Element { Text = ((ModuleDescriptor)x).Name, Tag = x, Children = convertSub((ModuleDescriptor)x) }).ToArray();
        }
    }
}
